package utils;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.widget.Toast;



import com.ma.R;
import com.yanzhenjie.permission.AndPermission;


/**
 * @author Wangli
 * @Description: 拨打电话工具类
 * @date: 2015/5/26 17:52
 * @version: V1.0
 */
public class CallPhoneUtil {
    public static final int call_requestCode = 101;

    public static void callPhone(final Activity context, final String phone) {
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(context, "电话号码为空不能拨号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            AndPermission.with(context)
                    .requestCode(101)
                    .permission(Manifest.permission.CALL_PHONE)
                    .send();
            return;
        }
            show(context, phone);


    }

    private static void show(final Activity context, final String phone) {

        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context );
        builder.setTitle("您确定要拨打电话");
        builder.setMessage(phone);
        builder.setIcon(R.drawable.ic_call_24dp);

        builder.setPositiveButton(android.R.string.ok, (dialog, which) -> {
            Intent intent;
            intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            dialog.dismiss();
            context.startActivity(intent);
        });
        builder.show();



    }




}

